package com.ruoyi.sale.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.sale.domain.ErpWarehouseOut;
import com.ruoyi.sale.service.IErpWarehouseOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 出库单Controller
 */
@RestController
@RequestMapping("/xiaoshou/out")
public class ErpWarehouseOutController extends BaseController {

    @Autowired
    private IErpWarehouseOutService erpWarehouseOutService;

    /**
     * 查询出库单列表
     */
    @PreAuthorize("@ss.hasPermi('xiaoshou:out:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpWarehouseOut erpWarehouseOut) {
        startPage();
        List<ErpWarehouseOut> list = erpWarehouseOutService.selectErpWarehouseOutList(erpWarehouseOut);
        return getDataTable(list);
    }

    /**
     * 获取出库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('xiaoshou:out:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(erpWarehouseOutService.selectErpWarehouseOutById(id));
    }

    /**
     * 新增出库单
     */
    @PreAuthorize("@ss.hasPermi('xiaoshou:out:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ErpWarehouseOut erpWarehouseOut) {
        return toAjax(erpWarehouseOutService.insertErpWarehouseOut(erpWarehouseOut));
    }

    /**
     * 修改出库单
     */
    @PreAuthorize("@ss.hasPermi('xiaoshou:out:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ErpWarehouseOut erpWarehouseOut) {
        return toAjax(erpWarehouseOutService.updateErpWarehouseOut(erpWarehouseOut));
    }

    /**
     * 删除出库单
     */
    @PreAuthorize("@ss.hasPermi('xiaoshou:out:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(erpWarehouseOutService.deleteErpWarehouseOutByIds(ids));
    }

    /**
     * 完成出库操作
     */
    @PreAuthorize("@ss.hasPermi('xiaoshou:out:complete')")
    @PutMapping("/complete/{id}")
    public AjaxResult complete(@PathVariable Long id) {
        // 1. 更新出库单状态为"已完成"
        erpWarehouseOutService.completeOut(id);

        // 2. 添加到销售汇总表
        erpWarehouseOutService.addToSalesSummary(id);

        return success("出库完成并已添加到销售汇总表");
    }

    @PutMapping("/ship")
    public AjaxResult ship(@RequestBody ErpWarehouseOut out) {
        out.setStatus("已发货");
        out.setShipTime(new Date());
        return toAjax(erpWarehouseOutService.updateErpWarehouseOut(out));
    }

    @GetMapping("/logisticsList")
    public AjaxResult getLogisticsCompanies() {
        // 假设从数据库查询物流公司列表
        List<Map<String, Object>> companies = erpWarehouseOutService.getLogisticsCompanies();
        return success(companies);
    }



}
